class Food {
  constructor() {
    this.x = 0;
    this.y = 0;
    this.points = 1;
    this.color = '#FF0000';
    this.size = 15;
    this.randomizePoints();
    this.randomizePosition();
  }

  randomizePoints() {
    const random = Math.random() * 100;
    if (random < 1) {
      this.points = 10;
      this.color = '#FF4500';
    } else if (random < 6) {
      this.points = 5;
      this.color = '#FF8C00';
    } else if (random < 26) {
      this.points = 3;
      this.color = '#FFA500';
    } else {
      this.points = 1;
      this.color = '#FF0000';
    }
  }

  randomizePosition(snakeBody = []) {
    const gridSize = 30;
    const maxX = Math.floor(800 / gridSize);
    const maxY = Math.floor(600 / gridSize);
    
    let newX, newY, isValidPosition;
    
    do {
      newX = Math.floor(Math.random() * maxX) * gridSize;
      newY = Math.floor(Math.random() * maxY) * gridSize;
      
      // 检查新位置是否与蛇身重叠
      isValidPosition = !snakeBody.some(segment => 
        segment.x === newX && segment.y === newY
      );
    } while (!isValidPosition && snakeBody.length > 0);
    
    this.x = newX + 7;
    this.y = newY + 12;
  }

  draw(ctx) {
    // 绘制苹果
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x + this.size/2, this.y + this.size/2, this.size/2, 0, Math.PI * 2);
    ctx.fill();
    
    // 苹果柄
    ctx.fillStyle = '#8B4513';
    ctx.fillRect(this.x + this.size/2 - 2, this.y - 5, 4, 5);
    
    // 苹果叶子
    ctx.fillStyle = '#32CD32';
    ctx.beginPath();
    ctx.moveTo(this.x + this.size/2, this.y - 5);
    ctx.lineTo(this.x + this.size/2 + 5, this.y - 10);
    ctx.lineTo(this.x + this.size/2 + 10, this.y - 5);
    ctx.fill();
  }
}

export { Food };